<template>
    <div class="map-marker">
        <div class="btns">
            <el-button @click="addMarker">add primary marker</el-button>
            <el-button @click="addSelfMarker">add self marker</el-button>
            <el-button @click="removeMarker">remove all marker</el-button>
        </div>
        <L7Map map-id="my-map" @loaded="handleLoaded" :options></L7Map>
    </div>
</template>

<script setup lang="ts">
import MapPopop from '@/components/MapPopop.vue';
import { Scene, Marker, Popup } from '@antv/l7';

const options = {

}
let myScene: Scene

const clickMarker = new Marker({ color: '#006dff' })
const clickPopup = new Popup({ offsets: [0, 50], closeButton: false })
const handleLoaded = (scene: Scene) => {
    myScene = scene
    myScene.on('click', ({ lngLat }) => {
        const { lng, lat } = lngLat
        clickMarker.remove()
        myScene.addMarker(
            clickMarker.setLnglat({ lng, lat })
        )
        myScene.addPopup(clickPopup.setText(`${lng},${lat}`).setLngLat({ lng, lat }).setMaxWidth('auto'))
    })
}

const addMarker = () => {
    for (let i = 0; i < 5; i++) {
        myScene.addMarker(
            new Marker({ draggable: true })
                .setLnglat({ lng: Math.random() * 80, lat: Math.random() * 80 })
                .setPopup(new Popup({ anchor: 'left', text: 'primary' }))
        )
    }
}

const addSelfMarker = () => {
    const div = document.createElement('div')
    div.id = 'self-marker'
    createApp(MapPopop, { infos: { s: 12222222, b: 33333333 } }).mount(div)
    myScene.addMarker(
        new Marker({ draggable: true, element: div })
            .setLnglat({ lng: Math.random() * 80, lat: Math.random() * 80 })
            .setPopup(new Popup({ anchor: 'left', text: 'self' }))
    )
    // myScene.remove
}

const removeMarker = () => {
    myScene.removeAllMarkers()
}
</script>

<style lang="less" scoped>
.map-marker {
    position: relative;
    height: 100%;

    .btns {
        position: absolute;
        left: 10px;
        top: 10px;
        z-index: 99999;

    }
}
</style>